package ua.kharkov.gritsay.net;

import java.io.IOException;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class demo {

	public static void main(String[] args) throws IOException {

		// String str = "фыва пролджэ. йцукен гшщзхъ: ячсмить бю.";
		// String resultStr = str.substring(str.indexOf('.') + 1,
		// str.indexOf(':'));

		// System.out.println(resultStr);

		Document doc;

		String url = "http://www.proxynova.com/proxy-server-list/anonymous-proxies/";

		doc = Jsoup.connect(url).get();

		// get ip`s
		Elements proxyIpAdres = doc.select("span.row_proxy_ip script"); // ip
		Elements proxyPort = doc.select("td a"); // port

		// regexp to find
		Pattern ipRegexp = Pattern
				.compile("\\d{1,3}.\\d{1,3}.\\d{1,3}.\\d{1,3}"); // ip
		Pattern portRegexp = Pattern.compile(">\\d{2,4}"); // port

		// port
		String inStr = "<a href=\"http://www.proxynova.com/proxy-server-list/port-";
		String outStr = "/\"";
		String resultStr = "";

		int count1 = 1;
		int count2 = 1;
		
		for (int i = 0; i <= proxyIpAdres.size(); i++) {
			Matcher prt = portRegexp.matcher(proxyIpAdres.toArray().toString());
			int counter = 1;
			if (prt.find()) {
				String s2[i] = prt.group();
				System.out
						.println(count1 + ": " + s2.substring(1, s2.length()));
				count1++;
			}
		}

		// find port`s in document
		for (Element port : proxyPort) {

			Matcher prt = portRegexp.matcher(port.toString());
			int counter = 1;
			if (prt.find()) {
				String s2 = prt.group();
				System.out
						.println(count1 + ": " + s2.substring(1, s2.length()));
				count1++;
			}
		}

		// find ip`s in document
		for (Element ip : proxyIpAdres) {
			Matcher m = ipRegexp.matcher(ip.toString());

			if (m.find()) {
				String s2 = m.group();
				System.out.println(count2 + ": " + s2);
				count2++;
			}
		}
	}
}
